package com.evildog.workspace.system.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.util.WsAssert;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.DictDataListBO;
import com.evildog.workspace.system.application.bo.DictTypePageBO;
import com.evildog.workspace.system.application.convert.DictTypeConvert;
import com.evildog.workspace.system.application.dto.DictTypeDTO;
import com.evildog.workspace.system.application.dto.DictTypePageDTO;
import com.evildog.workspace.system.application.entity.DictType;
import com.evildog.workspace.system.application.entity.DictTypePageDO;
import com.evildog.workspace.system.application.enums.DictTypeCodeEnum;
import com.evildog.workspace.system.application.enums.SystemCustomExceptionDesc;
import com.evildog.workspace.system.application.mapper.DictTypeMapper;
import com.evildog.workspace.system.application.service.DictDataService;
import com.evildog.workspace.system.application.service.DictTypeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {

    private final DictDataService dictDataService;

    @Override
    public PagedResult<DictTypePageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, DictTypePageDTO pageDTO) {
        IPage<DictTypePageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);

        List<DictTypePageDO> records = page.getRecords();

        DictType dictType = getOne(Wrappers.<DictType>lambdaQuery().eq(DictType::getCode, DictTypeCodeEnum.SYS_SERVICE.getCode()));
        WsAssert.notNull(dictType, SystemCustomExceptionDesc.MISSING_SYS_SERVICE);

        List<DictDataListBO> dataListList = dictDataService.getValidList(dictType.getId());
        Map<Long, DictDataListBO> dictDataListMap = dataListList.stream().collect(Collectors.toMap(DictDataListBO::getId, data -> data));

        records.forEach(dictDO -> {
            DictDataListBO dictDataListBO = dictDataListMap.get(dictType.getServiceId());
            dictDO.setServiceName(dictDataListBO.getName());
        });

        return DictTypeConvert.INSTANCE.convert(page);
    }

    @Override
    public boolean save(DictTypeDTO dto) {
        DictType newDictType = DictTypeConvert.INSTANCE.convert(dto);
        return save(newDictType);
    }

    @Override
    public boolean updateById(Long id, DictTypeDTO dto) {
        DictType oldDictType = getById(id);
        Assert.notNull(oldDictType, "dictType is null!");
        DictType newDictType = DictTypeConvert.INSTANCE.convert(dto);
        newDictType.setId(id);
        return updateById(newDictType);
    }
}
